/*
 * @Copyright: Citylife 2021
 * @Copyright: EasyCompany 2021
 * @Author: asterisk
 * @Date: 2021-03-15 16:15:32
 * @Description: 
 */
import '../../easy_log.dart';
import '../storage/dao.dart';
import '../utils.dart';

class EasyLogDBAppender implements EasyLogAppender {
  LogDao _dao;
  EasyLoggerConfig _config; //ref;
  EasyLogDBAppender(this._config) {
    _dao = createDao();
  }
  @override
  void logo(dynamic message) async {
    if (message is EasyLogMessage) {
      _dao.addOne(message);
    } else if (message is EasyLogInfo) {
      List<String> contents =
          _config.formatters['EasyLogInfoFormatter'].format(message);
      List<EasyLogMessage> messages = [];
      for (var content in contents) {
        messages.add(EasyLogMessage(
            tags: {
              'appName': EasyLoggerFactory.appName,
              'logName': _config.logName.isEmpty
                  ? EasyLoggerFactory.appName
                  : _config.logName,
            },
            timestamp: fromMillisecondsSinceEpoch(),
            level: EasyLogLevel.FLAG,
            message: content));
      }
      _dao.addMore(messages);
    }
  }

  @override
  void logs(String message) async {}

  @override
  Future init() async {}

  @override
  Future unInit() async {}
}
